{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "sharp-midnight",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import config\n",
    "import os \n",
    "import numpy as np\n",
    "import torch\n",
    "import copy\n",
    "from typing import Generator\n",
    "from matplotlib import pyplot as plt\n",
    "import sys\n",
    "sys.path.append(\"/gpfs/scratch/chgwang/XI/Scripts/MLModel\")\n",
    "import ParallelNet # type: ignore\n",
    "import matplotlib\n",
    "matplotlib.use(\"Agg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "demographic-merit",
   "metadata": {},
   "outputs": [],
   "source": [
    "def normalize(data:np.array):\n",
    "    data = np.array(data)\n",
    "    max_value = np.max(data)\n",
    "    min_value = np.min(data)\n",
    "    if max_value == min_value:\n",
    "        data = np.ones_like(data)\n",
    "    else:\n",
    "        data = (data-min_value) / (max_value - min_value) \n",
    "    return(data)\n",
    "\n",
    "def readFile(path:str):\n",
    "    # our sample step.\n",
    "    period = 0.02\n",
    "    period_points = 200\n",
    "    sampled_freq = period_points / period\n",
    "    # this data can got from the header.\n",
    "    # original sample rate\n",
    "    with open(path, mode=\"r\") as f:\n",
    "        while True:\n",
    "            line = f.readline()\n",
    "            if \"SampleRate\" in line:\n",
    "                break\n",
    "    line = line.split(\",\")\n",
    "    # Sample rate of the data.\n",
    "    source_freq = float(line[1].strip())\n",
    "    step = source_freq / sampled_freq\n",
    "    # convert to int for step only using integer\n",
    "    step = int(step)\n",
    "    # get the labels\n",
    "    path_splited = path.split(\"/\")\n",
    "    # start with . means is the hidden data\n",
    "    if path_splited[-1][0] == \".\":\n",
    "        # print(path)\n",
    "        return\n",
    "    labels = []\n",
    "    # \"0\" as the start name. \n",
    "    if path_splited[-1][0] == \"0\":\n",
    "        labels.append([\"0\"])\n",
    "    else:\n",
    "        labels.append([path_splited[-1][0]])\n",
    "        # isdigit is a function for str.m\n",
    "        if path_splited[-1][1].isdigit():\n",
    "            labels.append([path_splited[-1][1]])\n",
    "    # Now the labels is a list of list.\n",
    "    # read source data.\n",
    "    # source data shape is (timesteps, channels)\n",
    "    sour_data = np.loadtxt(path, skiprows=16, delimiter=\",\", usecols=range(1,4))\n",
    "    sour_data = sour_data.T\n",
    "    resampled_data = sour_data[:,::step]\n",
    "    resample_len = resampled_data.shape[1]\n",
    "    delimiter = int(resample_len / 2)\n",
    "    normal_label = [\"0\"] * delimiter\n",
    "    for idx, label_list in enumerate(labels):\n",
    "        label_list = label_list*(resample_len - delimiter)\n",
    "        temp_list = copy.deepcopy(normal_label)\n",
    "        temp_list.extend(label_list)\n",
    "        labels[idx] = temp_list\n",
    "    # convert labels to int\n",
    "    labels = np.array(labels).astype(int)\n",
    "    # norm_data = normalize(resampled_data)\n",
    "    # norm_data = torch.tensor(norm_data)\n",
    "    # labels = torch.tensor(labels, dtype=torch.int)\n",
    "    # return shape is \n",
    "    return(resampled_data, labels)\n",
    "\n",
    "def genPathData(path:str):\n",
    "    # normalize using in the \n",
    "    resampled_data, labels = readFile(path)\n",
    "    resampled_data_len = resampled_data.shape[1]\n",
    "    # if not add the \"1\" the result will be lost 1 sampled\n",
    "    for i in range(200, resampled_data_len+1):\n",
    "        sp_data = resampled_data[:,i-200:i]\n",
    "        sp_data = sp_data.T\n",
    "        sp_labels = labels[:,i-200:i]\n",
    "        sp_labels = sp_labels.T\n",
    "        sp_data = torch.tensor(sp_data)\n",
    "        sp_data = torch.unsqueeze(sp_data, 0)\n",
    "        sp_data = torch.unsqueeze(sp_data, 1)\n",
    "        sp_labels = torch.tensor(sp_labels)\n",
    "        sp_labels = torch.unsqueeze(sp_labels, 0)\n",
    "        sp_labels = torch.unsqueeze(sp_labels, 1)\n",
    "        sp_data = sp_data.float()\n",
    "        yield(sp_data, sp_labels)\n",
    "\n",
    "def retrieve_files(path:str) -> Generator:\n",
    "    path_gen = os.walk(path)\n",
    "    for root, _, files in path_gen:\n",
    "        for name in files:\n",
    "           yield(os.path.join(root,name))\n",
    "\n",
    "def modeledData(model, data_gen):\n",
    "    model_out_array = []\n",
    "    label_out_list = []\n",
    "    model.eval()\n",
    "    with torch.no_grad():\n",
    "        for sp_data, sp_label in data_gen:\n",
    "            model_out = model(sp_data)\n",
    "            model_out.cpu().numpy()\n",
    "            model_out = np.squeeze(model_out)\n",
    "            assert model_out.shape[0] == 6, \\\n",
    "            \"the model_out shapr is %s\"%model_out.shape\n",
    "            model_out_array.append(model_out.cpu().numpy())\n",
    "            label_out = torch.squeeze(sp_label.cpu()).numpy()\n",
    "            label_out_list.append(label_out[-1,:])\n",
    "            # label_out_list.append(torch.squeeze(sp_label.cpu()).numpy())\n",
    "        model_out_array = np.squeeze(model_out_array)\n",
    "        label_out_array = np.squeeze(label_out_list)\n",
    "    return(model_out_array, label_out_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "little-valley",
   "metadata": {},
   "outputs": [],
   "source": [
    "script_dir = \"/gpfs/scratch/chgwang/XI/Scripts/ML_validation\"\n",
    "basepath = os.path.dirname(os.path.dirname(scriptDir))\n",
    "dataDir = os.path.join(basepath, \"data\")\n",
    "dataDir = os.path.join(dataDir, \"邓茜实验数据\")\n",
    "file_gen = retrieve_files(dataDir)\n",
    "paths = list(file_gen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
